package cappuccino.couldbook.ui.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import cappuccino.couldbook.App;
import cappuccino.couldbook.R;
import cappuccino.couldbook.core.image.ImageReader;
import cappuccino.couldbook.data.Group;
import cappuccino.couldbook.ui.QueryNumberActivity;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/9/12.
 */
public class RecylerGroupAdapter extends RecyclerView.Adapter<RecylerGroupAdapter.InfoViewHolder> {

    private static final String TAG = "RecylerGroupAdapter";
    Group group;
    Context context;
    public RecylerGroupAdapter(Context context, Group group){
        this.group = group;
        this.context = context;
    }

    @Override
    public InfoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        InfoViewHolder holder;
        switch (viewType){
            case 0 :
                holder = new GroupViewHolder(LayoutInflater.from(App.getInstance().getApplicationContext()).inflate(R.layout.area_imagewithtitle,parent,false));
                break;
            default:
                holder = new UserViewHolder(LayoutInflater.from(App.getInstance().getApplicationContext()).inflate(R.layout.item_info,parent,false));
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(InfoViewHolder holder, int position) {
        holder.setInfo(position);
    }

    @Override
    public int getItemViewType(int position) {
        return position>0?1:0;
    }

    @Override
    public int getItemCount() {
        return group.users.size() + 1;
    }


    abstract class InfoViewHolder extends RecyclerView.ViewHolder{
        public InfoViewHolder(View itemView) {
            super(itemView);
        }
        abstract public void setInfo(int pos);
    }

    class UserViewHolder extends InfoViewHolder{
        TextView tv_user;
        ImageView iv_user;
        View view;
        public UserViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
            tv_user = (TextView) itemView.findViewById(R.id.tv_bar_text);
            iv_user = (ImageView) itemView.findViewById(R.id.iv_bar_photo);

        }

        @Override
        public void setInfo(final int pos) {
            tv_user.setText(group.users.get(pos-1).userName);
            Log.v(TAG,"pos:"+pos);
            Log.v(TAG,"userId:"+group.users.get(pos-1).id);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, QueryNumberActivity.class);
                    intent.putExtra("userId",group.users.get(pos-1).id);
                    context.startActivity(intent);
                }
            });
        }
    }
    class GroupViewHolder extends InfoViewHolder{
        TextView tv_name;
        ImageView iv_photo;
        View view;
        public GroupViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
            tv_name = (TextView) itemView.findViewById(R.id.tv_area_iwt_t);
            iv_photo = (ImageView) itemView.findViewById(R.id.iv_area_iwt_i);
        }

        @Override
        public void setInfo(final int pos) {
            ImageReader.getReader(R.drawable.back).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<Bitmap>() {
                        @Override
                        public void call(Bitmap bitmap) {
                            iv_photo.setImageBitmap(bitmap);
                        }
                    });

        }

    }
}
